package com.copsoftware.mantenimientos.acusado.dao;

import java.util.Collection;

import com.copsoftware.comun.beans.BeanDelitoAcusado;
import com.copsoftware.db.DAOSession;
import com.copsoftware.entity.DenAcusadoImpl;
import com.copsoftware.utils.beans.BeanBuilder;

public class AcusadoDAO extends DAOSession{

	
	public void guardarAcusado(DenAcusadoImpl denAcusado) {

		getSession().saveOrUpdate(denAcusado);
		getSession().flush();
	}
	
	public DenAcusadoImpl buscarAcusado(Integer cAcusado)
	{
		StringBuffer hql = new StringBuffer();
		hql.append("from DenAcusadoImpl where CPersona=").append(cAcusado);
		return (DenAcusadoImpl) getSession().createQuery(hql.toString()).uniqueResult();
	}

	public Collection buscarDelitoConAcusado(Integer cAcusado) throws Exception
	{
		StringBuffer hql = new StringBuffer();
		hql.append("select new map(d.CTipoDelito as codigo, td.XTipoDelito as descripcion, count(d.CTipoDelito) as cantidad) from DenDenunciaImpl d ");
		hql.append("		left join d.denTipoDelito td ");
		hql.append("		left join d.denInvestigacion i ");
		hql.append("		left join d.denInvestigacion.denAcusadoInvestigacions a ");
		hql.append("		where exists elements(i.denAcusadoInvestigacions) ");
		hql.append(" 		and a.id.CAcusado = ").append(cAcusado);
		hql.append("    	group by  d.CTipoDelito, td.XTipoDelito");
		
		Collection col = getSession().createQuery(hql.toString()).list();	
		return 	BeanBuilder.procesarFila(col, BeanDelitoAcusado.class);
	}
}
